<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>新增字典</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <input type="hidden" id="pid" name="pid">
        <label class="layui-form-label required">父级字典</label>
        <div class="layui-input-block">
            <input type="text" id="pidDictName" name="pidDictName" lay-verify="required"  placeholder="请选择父级字典" value="" class="layui-input" readonly>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">字典名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" lay-reqtext="" placeholder="请输入字典名称" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">字典值</label>
        <div class="layui-input-block">
            <input type="text" name="code" lay-verify="required" lay-reqtext="" placeholder="请输入字典值" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">排序号</label>
        <div class="layui-input-block">
            <input type="text" name="num" lay-verify="required|number" lay-reqtext="" placeholder="请输入排序号" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">字典备注</label>
        <div class="layui-input-block">
            <input type="text" name="tips" lay-verify="required" lay-reqtext="" placeholder="请输入字典备注" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">字体颜色</label>
        <div class="layui-input-block">
            <input type="text" name="color" lay-verify="required" lay-reqtext="" placeholder="请设置字体颜色" value="green" class="layui-input">
            <span class="layui-btn layui-btn-primary test-select-color" style="padding:0 12px;min-width:45px;background-color: green;"></span>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="https://cdn.bootcss.com/blueimp-md5/2.12.0/js/md5.min.js"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../js/layutil.js" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../lib/jq-module/paigusu.min.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'tableSelect'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$,
            tableSelect = layui.tableSelect;

        $('.test-select-color').paigusu({
            color: 'green',//初始色  支持两种配置方案
        }, function (event, obj) {
            //console.log(event);
            //console.log(obj);
            $(event).css('background-color', '#' + obj.hex);
            $('input[name="color"]').val('#' + obj.hex);
        });

        tableSelect.render({
            elem: '#pidDictName',
            checkedKey: 'id',
            searchType: 'more', //开启多条件搜索
            searchList: [
                {searchKey: 'tips', searchPlaceholder: '字典备注搜索'},
                {searchKey: 'name', searchPlaceholder: '字典名称搜索'},
            ],
            table: {
                url: '/myerp/dict/table',
                type: "GET",
                dataType:"json",
                headers:{'Authorization':'Bearer '+ JSON.parse(localStorage.getItem('userdetail')).token},
                cols: [[
                    { type: 'radio' },
                    { field: 'id', title: '编号' },
                    { field: 'tips', title: '字典备注' },
                    { field: 'name', title: '字典名称' }
                ]]
            },
            done: function (elem, data) {
                console.log(data);
                $("#pid").val(data.data[0].id);
                $("#pidDictName").val(data.data[0].tips);
                /*var NEWJSON = []
                layui.each(data.data, function (index, item) {
                    NEWJSON.push(item.username)
                })
                elem.val(NEWJSON.join(","))*/
            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var str = "确定新增字典？";
            layer.confirm(str, {btn: ['确定', '取消'], title: "提示"}, function (index) {
                $.ajax({
                    url:"/myerp/dict/add",
                    type:"POST",
                    dataTpe:"json",
                    data:data.field,
                    headers:{'Accept': 'application/json','Authorization':'Bearer '+ JSON.parse(localStorage.getItem('userdetail')).token},
                    success:function (res) {
                        if(res.code == '0') {
                            layutil.success("新增字典成功",1, function () {
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                            });
                            return false;
                        }
                        layutil.fail("新增字典失败 " + res.msg);
                    },error:function (data) {
                        console.log(JSON.stringify(data));
                        layutil.fail("新增字典失败");
                    }
                });


            });
            return false;
        });

    });
</script>
</body>
</html>